package com.scada.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.contant.MessageConstants;
import com.scada.model.bean.Dictionary;
import com.scada.model.bean.DictionaryData;
import com.scada.model.bean.vo.DictionaryVO;
import com.scada.model.result.ActionResult;
import com.scada.model.result.PageInfo;
import com.scada.service.DicService;



/**
 * 字典管理
 * 
 * @author 杨渊
 *
 * 2016年11月1日
 */
@Controller
@RequestMapping("dictionary")
public class DictionaryController extends BaseController{
	@Autowired
	private DicService dictionaryService;
	
	protected Logger logger = Logger.getLogger("DictionaryController");
	
	/**
	 * 
	 * @Title: toDictionary 
	 * @Description: 字典数据页面跳转
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:55:45
	 */
	@RequestMapping("toDictionary")
	public ModelAndView toDictionary(){
		//根据当前登录用户，判断是否为“admin”管理员分配是否可以管理系统字典
		if(getCurrentUser().getLoginName().equals("admin")){
			return new ModelAndView("system/dictionary").addObject("user",0);
		}else{
			return new ModelAndView("system/dictionary").addObject("user",1);
		}
	}
	
	/**
	 * 
	 * @Title: listData 
	 * @Description: 字典列表数据入口
	 * @param request
	 * @param pageInfo
	 * @param dictionary
	 * @return
	 * @return: String
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:57:25
	 */
	@RequestMapping("listData")
	@ResponseBody
	public String listData(HttpServletRequest request,@ModelAttribute PageInfo<Dictionary> pageInfo,Dictionary dictionary){
		pageInfo.setPageSize(Integer.parseInt(request.getParameter("length")));
		dictionary.setState(MessageConstants.State.STATE_SHOW.getKey());
		pageInfo.setCondition(dictionary);
		pageInfo = dictionaryService.selectAll(pageInfo);
		return toJson(pageInfo);
	}
	
	/**
	 * 
	 * @Title: insertSelective 
	 * @Description: 字典新增入口
	 * @param dictionary
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:57:50
	 */
	@RequestMapping(value = "add")
	public ModelAndView insertSelective(Dictionary dictionary){
		
		ActionResult result = dictionaryService.insertSelective(dictionary);
		
		return result.getJsonModelView();
	} 
	
	/**
	 * 
	 * @Title: deleteByPrimaryKey 
	 * @Description: 字典逻辑删除
	 * @param ids
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:58:10
	 */
	@RequestMapping(value = "del")
	public ModelAndView deleteByPrimaryKey(String ids){
		
		ActionResult result = dictionaryService.deleteByPrimaryKey(ids);
		
		return result.getJsonModelView();
	}
	
	/**
	 * 
	 * @Title: updateByPrimaryKeySelective 
	 * @Description: 字典更新
	 * @param dictionary
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:58:26
	 */
	@RequestMapping(value = "update")
	public ModelAndView updateByPrimaryKeySelective(@ModelAttribute Dictionary dictionary){
		
		ActionResult result = dictionaryService.updateByPrimaryKeySelective(dictionary);
		
		return result.getJsonModelView();
	}
	
	/**
	 * 
	 * @Title: selectById 
	 * @Description: 根据ID查询字典数据
	 * @param id
	 * @return
	 * @return: Dictionary
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:58:42
	 */
	@RequestMapping(value = "selectById")
	@ResponseBody
	public Dictionary selectById(int id){
		
		return dictionaryService.selectByPrimaryKey(id);
	}
	
	/**
	 * 
	 * @Title: isSys 
	 * @Description: 判断当前登录用户字典查看权限
	 * @param type
	 * @return
	 * @return: String
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午4:59:05
	 */
	@RequestMapping(value = "isSys")
	@ResponseBody
	public String isSys(String type){
		int isSys = dictionaryService.selectIsSysByType(type);
		return String.valueOf(isSys);
	}
	
	/**
	 * 
	 * @Title: listDet 
	 * @Description: 字典详情列表数据
	 * @param request
	 * @param pageInfo
	 * @param type
	 * @param dictionaryVO
	 * @return
	 * @return: String
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午5:00:02
	 */
	@RequestMapping("listDet")
	@ResponseBody
	public String listDet(HttpServletRequest request,@ModelAttribute PageInfo<DictionaryVO> pageInfo,String type,DictionaryVO dictionaryVO){
		pageInfo.setPageSize(Integer.parseInt(request.getParameter("length")));
		dictionaryVO.setState(MessageConstants.State.STATE_SHOW.getKey());
		dictionaryVO.setType(type);
	    pageInfo.setCondition(dictionaryVO);
		pageInfo = dictionaryService.selectAllData(pageInfo);
		return toJson(pageInfo);
	}
	
	/**
	 * 
	 * @Title: addDet 
	 * @Description: 添加字典列表数据
	 * @param dictionary
	 * @param type
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午5:00:16
	 */
	@RequestMapping(value = "addDet")
	public ModelAndView addDet(DictionaryData dictionary,String type){
		dictionary.setType(type);
		ActionResult result = dictionaryService.insertSelectiveD(dictionary);
		
		return result.getJsonModelView();
	} 
	
	/**
	 * 
	 * @Title: delDet 
	 * @Description: 批量删除字典类型 
	 * @param ids
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午5:00:28
	 */
	@RequestMapping(value = "delDet")
	public ModelAndView delDet(String ids){
		
		ActionResult result = dictionaryService.deleteByPrimaryKeyD(ids);
		
		return result.getJsonModelView();
	}
	
	/**
	 * 
	 * @Title: updateDet 
	 * @Description: 更新字典类型
	 * @param dictionary
	 * @return
	 * @return: ModelAndView
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午5:01:01
	 */
	@RequestMapping(value = "updateDet")
	public ModelAndView updateDet(@ModelAttribute DictionaryData dictionary){
		
		ActionResult result = dictionaryService.updateByPrimaryKeySelectiveD(dictionary);
		
		return result.getJsonModelView();
	}
	
	/**
	 * 
	 * @Title: selectByIdDet 
	 * @Description: 根据ID查询字典类型
	 * @param id
	 * @return
	 * @return: DictionaryData
	 * @author: yangyuan 
	 * @date: 2016年12月7日 下午5:01:09
	 */
	@RequestMapping(value = "selectByIdDet")
	@ResponseBody
	public DictionaryData selectByIdDet(int id){
		
		return dictionaryService.selectByPrimaryKeyD(id);
	}
	
	@RequestMapping("findempJobName")
	@ResponseBody
	public List<DictionaryData> findempJobName(){
		
		return dictionaryService.findempJobName();
	}
	
	
	/**
	 * 
	 * @Title: findDictionaryType 
	 * @Description: 查询字典 
	 * @param type   type编码
	 * @return
	 * @return: List<DictionaryData>
	 * @author: yangyuan 
	 * @date: 2016年12月30日 下午2:10:05
	 */
	@RequestMapping("findDictionary")
	@ResponseBody
	public List<DictionaryData> findDictionaryType(String type){
		List<DictionaryData> list = null;
		if(type!=null && StringUtils.isNotBlank(type)){
			list = dictionaryService.findDictionaryType(type);
		}else{
			logger.error("参数错误");
		}
		return list;
	}
	
	@RequestMapping("getSelectInvestmentType")
	@ResponseBody
	public String getSelectInvestmentType(String pageSize,String currentPage,String searchName){
		
		return toJson(dictionaryService.getSelectDicPageDate(pageSize, currentPage, searchName, Dictionary.TypeKeyCode.investment_type.getKey()));
	}
	
	@RequestMapping("getSelectCustType")
	@ResponseBody
	public String getSelectCustType(String pageSize,String currentPage,String searchName){
		
		return toJson(dictionaryService.getSelectDicPageDate(pageSize, currentPage, searchName, Dictionary.TypeKeyCode.cust_type.getKey()));
	}
	
	
	@RequestMapping("getSelectLectrueType")
	@ResponseBody
	public String getSelectLectrueType(String pageSize,String currentPage,String searchName){
		
		return toJson(dictionaryService.getSelectDicPageDate(pageSize, currentPage, searchName, Dictionary.TypeKeyCode.lecture_type.getKey()));
	}
	
	
}
